<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------
namespace api\v1\model;


use think\facade\Db;
use think\facade\Log;
use think\Model;

class User extends Model
{
    protected $table = "cmf_user";
    protected $dateFormat = "Y/m/d H:i";
    protected $type = [
       'address' => 'array',
    //    'contact_setting' => 'array',
    //    'more' => 'array',
    //    'start_time'  =>  'timestamp',
    //    'end_time'  =>  'timestamp',
        // 'deleted_at'  =>  'datetime',
    ];
    //protected $autoWriteTimestamp = true;
    //protected $autoWriteTimestamp = "datetime";
    //type:1充值，2提现，3购买产品，4产品反利，5游戏，6分销，7平仓，8产品收益,9余额宝收益
    public static function money_change($uid,$num,$type,$remark)
    {
        $user = self::find($uid);
        $result = false;
        if ($user && !empty($num)) {
                if($type==9) {
                    $before = $user->yeb;
                    $after = $user->yeb + $num;
                    $user->yeb = $after;
                    $user->yeb_shouyi = $user->yeb_shouyi + $num;
                }
                else {
                    $before = $user->balance;
                    $after = $user->balance + $num;
                    $user->balance = $after;
                }
               // if($type == 8) {
                    //按日结算时，在此统计收益。到期还本付息时，记得外部统计收益
                 //   $user->shouyi = $user->shouyi + $num;
                //}
                $user->save();
                Moneylog::create([
                    'user_id' => $uid,
                    'user' => $user->user_login,
                    'money' => $num,
                    'ip' => request()->ip(),
                    'type' => $type,
                    'notice' => $remark,
                    'before_amount' => $before,
                    'after_amount' => $after
                ]);
                $result = true;
        }
        return $result;
    }
    //type:1:购买产品,2:兑换,3:抽奖
    public static function score_change($uid,$num,$type,$remark)
    {
        $user = self::find($uid);
        $result = false;
        if ($user && !empty($num)) {
            $before = $user->score;
            $after = $user->score + $num;
            $user->score = $after;
            if ($num > 0) {
                if($user->score_total) {
                    $user->score_total = $user->score_total + $num;
                }
                else {
                    $user->score_total = $user->score;
                }
                $level = Level::where("integral", "<=", $user->score_total)->order("id", "desc")->find();
                if ($user->level_id < $level->id) {
                    $user->level_id = $level->id;
                }
            }
            $user->save();
            Jifenlog::create([
                'user_id' => $uid,
                'number' => $num,
                'type' => $type,
                'remark' => $remark,
                'before_amount' => $before,
                'after_amount' => $after
            ]);
            $result = true;
        }
        return $result;
    }
    //all:true，返回1级到level级所有成员，false，只返回level级成员
    public static function team_member_uids($uids,$level=1,$all=false) {
        if(empty($uids)) {
            return [];
        }
        if(!is_array($uids)) {
            $uids = explode(",",$uids);
        }
        $member_uids = array_column(self::field("id")->whereIn("pid",$uids)->select()->toArray(),'id');
        if($level <= 1) {
            return $member_uids;
        }
        else {
            $child_ids = self::team_member_uids($member_uids,$level-1,$all);
            return $all?array_merge($member_uids,$child_ids):$child_ids;
        }

    }
    public static function team_members($uids,$level=1,$order_field="id",$order="desc") {
        $member_uids = self::team_member_uids($uids,$level);
        $members = self::whereIn("id",$member_uids)->order($order_field,$order)->select();
        return $members;
    }

    public function orders() {
        return $this->hasMany(Order::class);
    }
    public function jforders() {
        return $this->hasMany(Jifenorder::class);
    }
    public function level() {
        return $this->belongsTo(Level::class);
    }
    public function auth() {
        return $this->hasOne(Auth::class);
    }
    public function bank() {
        return $this->hasOne(Bank::class);
    }
}
